<?php
class Util
{
  public static function countDim($array)
  {
   if (is_array(reset($array))) {  
     $return = Util::countDim(reset($array)) + 1;
   } else {
     $return = 1;
   }
   return $return;
  }
  
  public static function getWeeklistForMonth($year, $month)
  {
    $firstday = gmmktime(0,0,0,$month,1,$year);
    $days = gmdate('t', $firstday);
    $weekday  = gmdate('w', $firstday);
    $week = 0;
    $weeks = array(array('', '', '', '', '', '', ''));
    for($day=1; $day <= $days; $day++, $weekday++) {
      if ($weekday == 7) {
        $weekday = 0;
        $week++;
        $weeks[$week] = array('', '', '', '', '', '', '');
      }
      $weeks[$week][$weekday] = "$day";
    }

    return $weeks;
  }
  
  public static function getWeeklistForYear($year)
  {
    $months = array();
    for($month = 1; $month <= 12; $month++) {
      $months[$month] = Util::getWeeklistForMonth($year, $month);
    }
    return $months;
  }
  
  public static function getMonday($day, $addWeek = 0)
  {
    $day = new DateTime($day);
    $weekday = $day->format('w') - 1 - $addWeek * 7;
    $day->modify("-${weekday} days");
    
    return $day->format('Y-m-d');
  }
  
  public static function getFriday($day)
  {
    $day = new DateTime($day);
    $weekday = 5 - $day->format('w');
    $day->modify("+${weekday} days");
    
    return $day->format('Y-m-d');
  }
  
  public static function myMultisort($values, $index)
  {
    foreach($values as $key => $row) {
      $val[$key] = $row[$index];
    }
    array_multisort($val, SORT_DESC, $values);
    return $values;
  }
}